//
//  TMQuiltView
//
//  Created by Bruno Virlet on 7/20/12.
//
//  Copyright (c) 2012 1000memories

//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
//  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//  DEALINGS IN THE SOFTWARE.
//


#import "TMPhotoQuiltViewCell.h"
#import "DateUtility.h"
#import "UIImageView+WebCache.h"

const CGFloat kTMPhotoQuiltViewMargin = 5;

@implementation TMPhotoQuiltViewCell

@synthesize photoView = _photoView;
@synthesize titleLabel = _titleLabel;

- (void)dealloc {
    [_photoView release], _photoView = nil;
    [_titleLabel release], _titleLabel = nil;
    
    [super dealloc];
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createView];
    }
    return self;
}

-(void)createView{
    
     self.backgroundColor = [UIColor clearColor];
    
    _cellImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _cellImageView.image = [UIImage imageNamed:@"share_kuang"];
    _cellImageView.userInteractionEnabled = YES;
    [_cellImageView.image stretchableImageWithLeftCapWidth:30 topCapHeight:50];
    [self addSubview:_cellImageView];

    _photoView = [[UIImageView alloc] init];
    _photoView.clipsToBounds = YES;
    _photoView.layer.cornerRadius = 5;
    _photoView.contentMode = UIViewContentModeScaleAspectFill;
    _photoView.clipsToBounds = YES;
    [_cellImageView addSubview:_photoView];
    
    _shareLabel = [[UILabel alloc] init];
    _shareLabel.backgroundColor = [UIColor clearColor];
    _shareLabel.font = [UIFont systemFontOfSize:13.0f];
    _shareLabel.textColor = [UIColor whiteColor];
    _shareLabel.backgroundColor = [UIColor orangeColor];
    _shareLabel.numberOfLines = 0;
    _shareLabel.textAlignment = NSTextAlignmentCenter;
    [_cellImageView addSubview:_shareLabel];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont systemFontOfSize:13.0f];
    _titleLabel.textColor = MAIN_BLACK_COLOR;
    _titleLabel.numberOfLines = 0;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [_cellImageView addSubview:_titleLabel];
    
    
    _iconView = [[UIImageView alloc]init];
    _iconView.backgroundColor = [UIColor clearColor];
    [_cellImageView addSubview:_iconView];

    _contentNum = [[UILabel alloc]init];
    _contentNum.backgroundColor = [UIColor clearColor];
    _contentNum.font = [UIFont systemFontOfSize:11.f];
    _contentNum.textColor = [UIColor lightGrayColor];
    _contentNum.textAlignment = NSTextAlignmentLeft;
    [_cellImageView addSubview:_contentNum];
    
    
    _timeLabel = [[UILabel alloc]init];
    _timeLabel.font = [UIFont systemFontOfSize:11.0f];
    _timeLabel.textColor = [UIColor lightGrayColor];
    _timeLabel.textAlignment = NSTextAlignmentRight;
    [_cellImageView addSubview:_timeLabel];


}

- (void)layoutSubviews {
    
    _cellImageView.frame = self.bounds;
    //图片
    if (self.indexPath %2 ==0) {
        self.photoView.frame = CGRectMake(kTMPhotoQuiltViewMargin, kTMPhotoQuiltViewMargin,
                                          self.bounds.size.width - 2 * kTMPhotoQuiltViewMargin, 160);
        
        
    }else{
        self.photoView.frame = CGRectMake(kTMPhotoQuiltViewMargin, kTMPhotoQuiltViewMargin,
                                          self.bounds.size.width - 2 * kTMPhotoQuiltViewMargin, 120);
    }
    
    
   
    
    //分享、投诉等label
    self.shareLabel.frame = CGRectMake(kTMPhotoQuiltViewMargin, self.photoView.bottom+2, 60, 20);
    CGFloat weith = [UIUtility getLabelWidthOfText:_shareLabel.text andFont:[UIFont systemFontOfSize:13]];
    _shareLabel.width =weith+5;
    
    //标题
    self.titleLabel.frame = CGRectMake(self.shareLabel.right, self.photoView.bottom,
                                       self.bounds.size.width - 2 * kTMPhotoQuiltViewMargin-self.shareLabel.width, 20);
    
    
    //收藏icon
    _iconView.frame =CGRectMake(_shareLabel.left, _titleLabel.bottom + 15, 15, 15);
    
    //收藏数
    _contentNum.frame = CGRectMake(_iconView.right+3, _iconView.top, 20, 20);
    
    _timeLabel.frame = CGRectMake(_contentNum.right, _contentNum.top, 80, 20);
   _timeLabel.right = self.width - 5;

}

-(void)setShareModel:(ShareModel *)shareModel{
    if (_shareModel != shareModel) {
        _shareModel = shareModel;
        
        [_photoView setImageWithURL:[NSURL URLWithString:shareModel.smallImgPath]];
        _titleLabel.text = shareModel.content;
        [_iconView setImage:[UIImage imageNamed:@"qipao_pingjia"]];
        
        
        
       if([shareModel.type isEqual:@"1"]){
            _shareLabel.text = @"意见反馈";
            _shareLabel.backgroundColor = [UIColor greenColor];
        }else if([shareModel.type isEqual:@"2"]){
            _shareLabel.text = @"表扬";
            _shareLabel.backgroundColor = [UIColor redColor];
        }else if([shareModel.type isEqual:@"3"]){
            _shareLabel.text = @"好人好事";
            _shareLabel.backgroundColor = [UIColor redColor];
        }else if([shareModel.type isEqual:@"4"]){
            _shareLabel.text = @"帮帮忙";
            _shareLabel.backgroundColor = [UIColor purpleColor];
        }else if([shareModel.type isEqual:@"5"]){
            _shareLabel.text = @"跳蚤市场";
            _shareLabel.backgroundColor = [UIColor purpleColor];
        }else if([shareModel.type isEqual:@"6"]){
            _shareLabel.text = @"美食";
            _shareLabel.backgroundColor = [UIColor orangeColor];
        }else if([shareModel.type isEqual:@"7"]){
            _shareLabel.text = @"其它";
            _shareLabel.backgroundColor = [UIColor brownColor];
        }
        
        _contentNum.text = shareModel.commentNumber;
        
        _titleLabel.text = shareModel.content;
        
        NSString *str = [NSString stringWithFormat:@"%.0f",[DateUtility timeIntervalByDateString:shareModel.createTime withDateFormat:@"yyyy-MM-dd HH:mm:ss"]];
        
        _timeLabel.text = [DateUtility timeStampTimeValueForKey:str];
        _timeLabel.right = self.width - 5;
        
    }
}
@end
